﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AteneoLibriBusinessLogic.Entities;
using AteneoLibriBusinessLogic.MyException;

namespace AteneoLibriBusinessLogic.DAL
{
    public class UserFacade
    {

        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<User> getUsers()
        {
            List<User> list = new List<User>();
            try
            {
                IEnumerable<AteneoLibriEntityFramework.User> users = new List<AteneoLibriEntityFramework.User>();
                users = db.Users.ToList();

                foreach (var item in users)
                {
                    User usr = TOA.UserAssembler.Assembler(item);
                    list.Add(usr);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR: [METHOD: getUsers()] " + ex.Message);
            }

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public User getUser(string email)
        {
            AteneoLibriEntityFramework.User userEF = db.Users.Where(x => x.Email == email).FirstOrDefault();
            User user = null;
            if (userEF != null)
            {
                user = TOA.UserAssembler.Assembler(userEF);
            }

            return user;
        }

        public bool CheckExistingRegistration(string email)
        {
            return db.Users.Where(x => x.Email.ToLower() == email).Count() == 1;
        }

        public bool UserNameExist(string username)
        {
            return db.Users.Where(x => x.Username.ToLower() == username).Count() > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User getUsernameByEmailAndPsw(string email, string password)
        {
            AteneoLibriEntityFramework.User userEF = db.Users.Where(x => x.Email == email && x.Password == password).FirstOrDefault();
            User user = null;
            if (userEF != null)
            {
                user = TOA.UserAssembler.Assembler(userEF);
            }

            return user;
        }

        /// <summary>
        /// Utilizzato nel recupero password
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public User getUserByEmail(string email)
        {
            AteneoLibriEntityFramework.User userEF = db.Users.Where(x => x.Email == email).FirstOrDefault();
            User user = null;
            if (userEF != null)
            {
                user = TOA.UserAssembler.Assembler(userEF);
            }
            return user;
        }

        public User getUserById(Guid idUser)
        {
            AteneoLibriEntityFramework.User userEF = db.Users.Where(x => x.IdUser == idUser).FirstOrDefault();
            User user = null;
            if (userEF != null)
            {
                user = TOA.UserAssembler.Assembler(userEF);
            }
            return user;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool insertUser(User user)
        {
            bool result = true;
            AteneoLibriEntityFramework.User userEF = TOA.UserAssembler.DeAssembler(user);
            try
            {
                db.Users.AddObject(userEF);
                db.SaveChanges();
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool modifyUserAccessDate(User user)
        {
            bool result = true;
            try
            {
                var u = db.Users.Where(x => x.IdUser == user.IdUser).FirstOrDefault();
                if (u != null)
                {
                    u.LastAccess = user.LastAccess;
                }

                db.SaveChanges();
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        public bool updatePassword(User user)
        {
            bool result = true;
            try
            {
                var u = db.Users.Where(x => x.IdUser == user.IdUser).FirstOrDefault();
                if (u != null)
                {
                    u.Password = user.Password;
                }

                db.SaveChanges();
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        public bool updateUser(User user)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.User userEF = db.Users.Where(x => x.IdUser == user.IdUser).FirstOrDefault();
                //saleEF = TOA.SaleAssembler.DeAssembler(sale);
                TOA.UserAssembler.WriteToEntity(userEF, user);

                db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public List<User> getUsersByUniversityId(Guid universityId)
        {
            List<User> users = new List<User>();
            List<AteneoLibriEntityFramework.User> usersEF = db.Users.Where(x => x.UniversityId == universityId).ToList();

            foreach (var item in usersEF)
            {
                User user = TOA.UserAssembler.Assembler(item);
                users.Add(user);
            }

            return users;
        }

        public bool deleteUser(Guid userId)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.User user = db.Users.Where(x => x.IdUser == userId).First();

                db.Users.DeleteObject(user);
                db.SaveChanges();
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }
    }
}
